<?
  require_once('polypack.php');

  function geometry_to_array($s, $encode = false) {
    $splits = split('[()]', trim($s));
    $type = $splits[0];
    if ($type == 'POINT')
      return point_to_array($splits[1]);
    elseif ($type == 'LINESTRING')
      return line_to_array($splits[1], $encode);
    elseif ($type == 'MULTILINESTRING')
      return multiline_to_array($splits, $encode);
    elseif ($type == 'POLYGON')
      return polygon_to_array($splits, $encode);
    else
      return null;
  }

  function point_to_array($s) {
    return split(' ', trim($s));
  }

  function line_to_array($s, $encode) {
    $points = array();
    $pp = split(',', trim($s));
    foreach($pp as $point)
      $points[] = split(' ', trim($point));
    if ($encode)
      return "'".poly_encode($points)."'";
    else
      return $points;
  }

  function polygon_to_array($splits, $encode) {
    $c = (count($splits)-3)/2;
    $lines = array();
    $ss = '';
    for ($i=0; $i<$c; $i++) {
      $line = line_to_array($splits[2+2*$i], $encode);
      $lines[] = $line;
    }
    return $lines;
  }

  function multiline_to_array($splits, $encode) {
    return polygon_to_array($splits, $encode);
  }

?>
